package org.android;

import java.net.URLDecoder;
import java.util.ArrayList;

import org.helloroad.Trip;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class TripListView extends ListActivity {
	// private static final int RETRIEVE_TRIP_ID = Menu.FIRST;
	// private static final int VIEW_CONTACT_ID = Menu.FIRST + 1;
	private static final int INSERT_ID = Menu.FIRST;
	private static final int SEARCH_ID = Menu.FIRST + 1;
	private static final int DELETE_ID = Menu.FIRST + 1;
	private static final int ACTIVITY_CREATE = 0;
	private Button back;
	private int userId;
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trip_list);

		Bundle extras = getIntent().getExtras();
		userId = extras.getInt("userId");
		back = (Button) findViewById(R.id.backFromTripList);
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				goToWelcome();
			}
		});
		
		ArrayList<Trip> tripList = WSCommunicator.getListOfTrips(userId);
		for (Trip trip : tripList) {
			trip.setFrom(URLDecoder.decode(trip.getFrom()));
			trip.setTo(URLDecoder.decode(trip.getTo()));
		}
		
		setListAdapter(new ArrayAdapter<Trip>(this,
				android.R.layout.simple_list_item_1, tripList));
		
		registerForContextMenu(getListView());
	}

	protected void goToWelcome() {
		Intent i = new Intent(this, WelcomeView.class);
		i.putExtra("userId", userId);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.add_trip);
		menu.add(0, SEARCH_ID, 1, R.string.search_trip);
		// menu.add(0, RETRIEVE_TRIP_ID, 0, R.string.import_trip);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case INSERT_ID:
			goToAddTrip();
			break;
		case SEARCH_ID:
			goToSearchTrip();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.delete_trip);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_ID:
			item.getMenuInfo();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Trip tripDetails = (Trip) l.getItemAtPosition(position);
		Intent i = new Intent(this, TripInfoView.class);
		i.putExtra("tripDetails", tripDetails);
		startActivityForResult(i, ACTIVITY_CREATE);

	}

	private void retrieveTrips() {
		
		/*
		 * String[] projections = new String[] { People._ID, People.NAME };
		 * String[] from = new String[] { People.NAME }; int[] to = new int[] {
		 * R.id.trip_row };
		 * 
		 * Uri contacts_uri = People.CONTENT_URI; mContactCursor =
		 * managedQuery(contacts_uri, projections, null, null, null);
		 * 
		 * SimpleCursorAdapter trips = new SimpleCursorAdapter(this,
		 * R.layout.trip_row, mContactCursor, from, to); setListAdapter(trips);
		 */
		Intent i = new Intent(this, TripInfoView.class);
		i.putExtra("id_trip", 1);
		startActivityForResult(i, ACTIVITY_CREATE);
	
	}

	private void goToAddTrip() {
		Intent i = new Intent(this, AddTripView.class);
		i.putExtra("userId", userId);
		startActivityForResult(i, ACTIVITY_CREATE);
		
	}

	private void goToSearchTrip() {
		Intent i = new Intent(this, SearchTripView.class);
		i.putExtra("userId", userId);
		startActivityForResult(i, ACTIVITY_CREATE);
	}
}
